package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA
 * Created By Kinsey
 * Date: 2017/12/26
 * Time: 22:59
 */
@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {

    private Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ServerResponse addCategory(String categoryName, Integer parentId) {
        if(StringUtils.isBlank(categoryName) || parentId == null){
            return ServerResponse.createByErrorOnMsg("添加分类信息参数有误");
        }
        Category category = new Category();
        category.setId(parentId);
        category.setName(categoryName);
        category.setStatus(true);
        int insert = categoryMapper.insert(category);
        if(insert > 0 ){
            return ServerResponse.createBySuccessOnMsg("添加分类信息成功");
        }
        return ServerResponse.createByErrorOnMsg("添加分类信息失败");
    }

    public ServerResponse updateCategory(String categoryName ,Integer categoryId){
        if(StringUtils.isBlank(categoryName) || categoryId == null){
            return ServerResponse.createByErrorOnMsg("修改分类信息参数有误");
        }
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);
        int count = categoryMapper.updateByPrimaryKeySelective(category);
        if(count > 0 ){
            return ServerResponse.createBySuccessOnMsg("修改分类信息成功");
        }
        return ServerResponse.createByErrorOnMsg("修改分类信息参数有误");
    }

    public ServerResponse<List<Category>> getChilderCategory(Integer categoryId){
        List<Category> categoryList = categoryMapper.selectCategoryChilderByParentId(categoryId);
        if(CollectionUtils.isEmpty(categoryList)){
            logger.info("未找到当前分类的子分类");
        }
        return ServerResponse.createBySuccess(categoryList);
    }

    public ServerResponse selectCategoryAndChilderById(Integer categoryId){
        Set<Category> categorySet = Sets.newHashSet();
        findChilderCategory(categorySet,categoryId);

        List<Integer> categoryIdList = Lists.newArrayList();
        if(categoryId != null ){
            for(Category categoryItem : categorySet){
                categoryIdList.add(categoryItem.getId());
            }
        }

        return ServerResponse.createBySuccess(categoryIdList);
    }


    //递归算法，求子节点
    public Set<Category> findChilderCategory(Set<Category> categorySet , Integer id){
        Category category = categoryMapper.selectByPrimaryKey(id);
        if(category != null){
            categorySet.add(category);
        }
        //查找子节点
        List<Category> categorieList = categoryMapper.selectCategoryChilderByParentId(id);
        for (Category c : categorieList){
            findChilderCategory(categorySet,c.getId());
        }
        return categorySet;
    }
}
